package by.semerikov.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import by.semerikov.constants.ProposalStates;
import by.semerikov.constants.Results;
import by.semerikov.exceptions.ConnectorException;
import by.semerikov.exceptions.DAOException;
import by.semerikov.form.AgreeWithProposalForm;
import by.semerikov.model.Person;
import by.semerikov.model.Proposal;
import by.semerikov.service.ProposalService;
import by.semerikov.service.ServiceFactory;

public class AgreeWithProposalAction extends SecuredAction {
	
	@Override
	public ActionForward executedInternal(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response , Person user) throws DAOException, ConnectorException {
		AgreeWithProposalForm _form = (AgreeWithProposalForm) form;
		Proposal newProposal = new Proposal(_form.getId(), null,null,null, null, null,null, null);
		ProposalService proposalService =  ServiceFactory.getInstance().getProposalService();
		List<Proposal> resultTrips = proposalService.getProposal(newProposal);
		if(resultTrips != null && resultTrips .size() >0 )
		{
			Proposal updatedProposal = resultTrips.get(0);
			if("true".equals(_form.getIsAgree()))
				updatedProposal.setState(ProposalStates.CONFIRM);
			else {
				updatedProposal.setState(ProposalStates.FREE);
			}
			proposalService.updateProposal(updatedProposal);
		}
		return mapping.findForward(Results.SUCCESS);
	}
}
